import { Button, Modal } from 'antd'
import { useNavigate } from 'react-router-dom'
import {ExclamationCircleOutlined} from "@ant-design/icons";
import {newsDeleteOneApi, NewsEditOneApi} from '../../../api'
export function useUpdateData(getTableData) {
   const navigate = useNavigate()
   const { confirm } = Modal
   const deleteOneNews = data => {
       return newsDeleteOneApi(data)
           .then(res => {
               if(res.success) {
                   window.$message.success(res.msg)
               }
           }).catch(e => {
               window.$message.error(e)
           }).finally(_ => getTableData())
   }
   const newsUpdateBtns = (row = {}) => {
       const { newsid,status } = row
       const handleDel = () =>{
           confirm({
               icon: <ExclamationCircleOutlined />,
               title: '提示',
               content: '确认要删除该内容吗？',
               centered: true,
               onOk() {
                   deleteOneNews({ newsid })
               }
           })
       }
       const handleToEdit = () => {
           navigate(`/news/detail?page=edit&id=${ row.newsid }`)
       }
       const handleDownOrPublish = status =>{
           confirm({
               icon: <ExclamationCircleOutlined />,
               title: '提示',
               content: `确认要${ !status ? '下架' : '发布' }该内容吗？`,
               centered: true,
               onOk() {
                   return NewsEditOneApi({ newsid, status })
                       .then(res => {
                           if(res.success) {
                               window.$message.success(`${ !status ? '下架' : '发布' }成功`)
                           }
                       }).catch(e => {
                           window.$message.error(e)
                       }).finally(_ => getTableData())
               }
           })
       }
       const handlePublish = () => {}
       return (
           <div className="btns">
               { !status && <Button className="btn" color="green" size="small" type="primary" onClick={handleToEdit}>编辑</Button> }
               { !status &&  <Button className="btn" type="primary" danger size="small" onClick={handleDel}>删除</Button> }
               { !!status && <Button className="btn" type="primary" danger size="small" onClick={() =>handleDownOrPublish(0)}>下架</Button> }
               { !status && <Button className="btn" type="primary" size="small" onClick={() =>handleDownOrPublish(1)}>发布</Button> }
           </div>
       )
   }
   return {
       newsUpdateBtns
   }
}
